﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NS.Utilities;
using NS.BLL.Systems;
using NS.Model.Systems;

public partial class Systems_AreaManager : System.Web.UI.Page
{
    private string area = "000000";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["area"] != null)
        {
            area = Request.QueryString["area"];
        }
        LabPath.Text = CreatePath(area);

        if (!IsPostBack)
        {
            ((Label)Master.FindControl("PageTitle")).Text = "地区设置";
            GridViewDataBind();
        }
    }
    private string CreatePath(string AreaNumber)
    {
        string Pages = "AreaManager.aspx?mid=" + Request.QueryString["mid"];
        string strPath = "<a href=" + Pages + "&area=000000>全国</a>>>";
        string sheng = string.Empty;
        string shi = string.Empty;
        string xian = string.Empty;

        if (AreaNumber.Length == 6 && AreaNumber != "000000")
        {
            sheng = AreaNumber.Substring(0, 2) + "0000";
            shi = AreaNumber.Substring(0, 4) + "00";
            xian = AreaNumber;
            if (AreaNumber.Substring(2, 2) == "00")
            {
                strPath += "<a href=" + Pages + "&area=" + sheng + ">" + AreaService.getAreaName(sheng) + "</a>>>";
            }
            else if (AreaNumber.Substring(4, 2) == "00")
            {
                strPath += "<a href=" + Pages + "&area=" + sheng + ">" + AreaService.getAreaName(sheng) + "</a>>>";
                strPath += "<a href=" + Pages + "&area=" + shi + ">" + AreaService.getAreaName(shi) + "</a>>>";
            }
            else if (AreaNumber.Substring(4, 2) != "00")
            {
                strPath += "<a href=" + Pages + "&area=" + sheng + ">" + AreaService.getAreaName(sheng) + "</a>>>";
                strPath += "<a href=" + Pages + "&area=" + shi + ">" + AreaService.getAreaName(shi) + "</a>>>";
                strPath += "<a href=" + Pages + "&area=" + xian + ">" + AreaService.getAreaName(xian) + "</a>>>";
            }
        }
        return strPath;
    }
    //绑定数据源
    private void GridViewDataBind()
    {
        FGridView1.SQLString = AreaService.GetAreaStr(area);
        FGridView1.PKey = "ID";
        FGridView1.OrderName = "AreaNumber";
        FGridView1.BindData();
        FGridView1.AddControl();
    }
    //删除数据
    protected void DelButton_Click(object sender, EventArgs e)
    {
        string ID = ((Button)sender).CommandArgument.ToString();
        AreaService.DeleteAreaById(int.Parse(ID));
        GridViewDataBind();
    }
    //修改后重新绑定数据
    protected void btnBindData_Click(object sender, EventArgs e)
    {
        GridViewDataBind();
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Area Area = new Area();
        Area.AreaNumber = txtAreaNumber.Text;
        Area.AreaName = txtAreaName.Text;
        Area.ShortAreaName = txtCarnumber.Text.Trim();

        if (AreaService.AddArea(Area) > 0)
        {
            txtAreaName.Text = "";
            GridViewDataBind();
        }
        else
        {
            MessageBox.ShowSaveError(this);
        }
    }
}